package com.mac.hosp_pre.user.intercepter;

import com.mac.hosp_pre.base.UserBaseDto;
import com.mac.hosp_pre.constant.CommonConstant;
import com.mac.hosp_pre.handler.RefreshTokenHandler;
import com.mac.hosp_pre.interceptor.PortalInterceptor;
import com.mac.hosp_pre.user.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class PortalTokenVerifyInterfaceptor implements PortalInterceptor {

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public UserBaseDto getUserInfo(String token, Long userId) {
        return  (UserBaseDto)redisUtil.getCacheObject(CommonConstant.PORTAL_LOGIN_TOKEN_KEY + userId + ":" + token.hashCode());
    }

    @Override
    public RefreshTokenHandler getRefreshTokenHandler() {
        return (loginRedisKey, expiration, userInfo) -> {
            redisUtil.setCacheObject(loginRedisKey, userInfo, expiration, TimeUnit.SECONDS);
        };
    }
}
